package com.servicecloud.consumer.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.servicecloud.entities.Dept;

@RestController
public class DeptController_Consumer {

	// private static final String REST_URL_PREFIX = "http://localhost:8001";
	private static final String REST_URL_PREFIX = "http://MICROSERVICECLOUD";// 通过真实的微服务进行调用
	@Autowired
	private RestTemplate restTemplate;
	@Autowired
	private HttpHeaders headers;// 注入头信息为SpringSecurity进行授权认证
	@Autowired
	private LoadBalancerClient loadBalancerClient;// 通过这个类可以获取负载节点的相关信息

	@RequestMapping(value = "/consumer/dept/add")
	public boolean add(Dept dept) {
		// 未加密配置
		// return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept,
		// Boolean.class);
		// 加密配置的代码
		return restTemplate.exchange(REST_URL_PREFIX + "/dept/add/", HttpMethod.POST,
				new HttpEntity<Object>(dept, this.headers), Boolean.class).getBody();
	}

	@RequestMapping(value = "/consumer/dept/get/{id}")
	public Dept get(@PathVariable("id") Long id) {
		try {
			/**
			 * note：如果没有进行安全认证就会报401 Unauthorized错误
			 */
			// restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id,
			// Dept.class);没有进行安全认证
			// 进行了SpringSecurity安全认证
			return restTemplate.exchange(REST_URL_PREFIX + "/dept/get/" + id, HttpMethod.GET,
					new HttpEntity<Object>(this.headers), Dept.class).getBody();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/consumer/dept/list")
	public List<Dept> list() {
		// return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list",
		// List.class);无安全配置
		// 使用了安全配置的请求
		return restTemplate.exchange(REST_URL_PREFIX + "/dept/list", HttpMethod.GET,
				new HttpEntity<Object>(this.headers), List.class).getBody();
	}

	// 测试@EnableDiscoveryClient,消费端可以调用服务发现
	@RequestMapping(value = "/consumer/dept/discovery")
	public Object discovery() {
		// return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery",
		// Object.class);
		// 使用了安全认证的请求
		return restTemplate.exchange(REST_URL_PREFIX + "/dept/discovery", HttpMethod.GET,
				new HttpEntity<Object>(this.headers), Object.class).getBody();
	}

	/**
	 * 作用：</br>
	 * 获取当前负载节点的相关信息
	 * 
	 * @Author Gavin</br>
	 * @return</br>
	 * 				2019年8月13日
	 */
	@GetMapping("/consumer/getLoadBalancerInfo")
	public Object getLoadBalancerInfo() {
		ServiceInstance serviceInstance = loadBalancerClient.choose("MICROSERVICECLOUD");
		return serviceInstance;
	}
}
